package com.dhl.cloud.course.service;


import cn.hutool.core.lang.UUID;
import com.dhl.cloud.common.util.DateUtils;
import com.dhl.cloud.common.util.WordUtil;
import com.dhl.cloud.course.dao.LessionTranscriptMapper;
import com.dhl.cloud.course.dao.LessionTranscriptmutMapper;
import com.dhl.cloud.course.dao.LessionTransfileMapper;
import com.dhl.cloud.course.entity.LessionTranscript;
import com.dhl.cloud.course.entity.LessionTranscriptmut;
import com.dhl.cloud.course.entity.LessionTransfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class LessionTranscriptmutService {
	@Autowired
	private LessionTranscriptmutMapper lessionTranscriptmutMapper;
	@Autowired
	private LessionTranscriptMapper lessionTranscriptMapper;
	@Autowired
	private LessionTransfileMapper lessionTransfileMapper;
	
	public List<LessionTranscriptmut> selectByLessionid(Integer lessionid) {
		return lessionTranscriptmutMapper.selectByLessionid(lessionid);
	}
	
	public int deleteByPrimaryKey(Integer id) {
		return lessionTranscriptmutMapper.deleteByPrimaryKey(id);
	}
	
	//翻译结束后合并LessionTranscript的中间结果，以10-15行为一个段落
	public void mergeTranscript(String lessionnum,String rootSavePath) {
		List<LessionTranscript> list = lessionTranscriptMapper.selectByLessionnum(lessionnum);
		if (list != null && list.size() > 0)
		{
			Random random = new Random();
			int randomNumber = random.nextInt(6) + 10;
			String strCon = "";
			String strEnCon = "";
			String ct = null;
			Integer courseid = list.get(0).getCourseid();
		    Integer lessionid = list.get(0).getLessionid();
			int size = list.size();
			for (int i=0;i<size;i++)
			{
				if (i<randomNumber)
				{
					strCon += list.get(i).getContent();
					strEnCon += list.get(i).getEncontent()+" ";
					if (ct == null)
					{
						ct = list.get(i).getCreatetime();
					}
					
				}
				else if (i==randomNumber)
				{
					
					strCon += list.get(i).getContent();
					strEnCon += list.get(i).getEncontent()+" ";
					LessionTranscriptmut c = new LessionTranscriptmut();					
					c.setContent(strCon);
					c.setEncontent(strEnCon);
					c.setCourseid(courseid);
					c.setLessionid(lessionid);
					c.setLessionnum(lessionnum);
					c.setCreatetime(ct);
					insert(c,rootSavePath);
					
					randomNumber += random.nextInt(6) + 10;
					strCon = "";
					strEnCon = "";
					ct = null;
				}
			}
			if (strCon.length() > 1)
			{
				LessionTranscriptmut c = new LessionTranscriptmut();					
				c.setContent(strCon);
				c.setEncontent(strEnCon);
				c.setCourseid(courseid);
				c.setLessionid(lessionid);
				c.setLessionnum(lessionnum);
				if (ct != null)
				{
					c.setCreatetime(ct);
				}
				insert(c,rootSavePath);
			}
		}
		
	}
	
	/**
	 *  插入一条翻译，就更新下word文档
	 * @param c
	 * @return
	 */
	public int insert(LessionTranscriptmut c,String rootSavePath) {
		String lessionnum = c.getLessionnum();
		if (c.getId() != null)
		{
			LessionTranscriptmut cc = lessionTranscriptmutMapper.selectByPrimaryKey(c.getId());
			cc.setContent(c.getContent());
			cc.setModifytime(DateUtils.getDateTime());
			lessionTranscriptmutMapper.updateByPrimaryKey(cc);
			lessionnum = cc.getLessionnum();
		}
		else
		{
			if (c.getCreatetime() == null)
			{
				c.setCreatetime(DateUtils.getDateTime());
			}
			lessionTranscriptmutMapper.insert(c);
		}
		
		
		List<LessionTransfile> ltflist = lessionTransfileMapper.selectByLessionnum(lessionnum);
		String sourcePath = null;
		LessionTransfile record = null;
		if (ltflist != null && ltflist.size() > 0)
		{
			sourcePath = rootSavePath + ltflist.get(0).getFilepath().replace("/Path", "");
			record = ltflist.get(0);
		}
		else
		{
			String str = UUID.randomUUID().toString();
			String temp = "/lessionfile/"+str + ".docx";
			sourcePath = rootSavePath + temp;
			record = new LessionTransfile();		
			record.setCourseid(c.getCourseid());
			record.setLessionid(c.getLessionid());
			record.setLessionnum(lessionnum);
			record.setFilename(str+".docx");
			record.setFilepath("/Path"+temp);
			record.setCreatetime(DateUtils.getDateTime());
		}
				
		Map<String, Object> params = new HashMap();
		List<LessionTranscriptmut> scriptlist = lessionTranscriptmutMapper.selectByLessionnum(lessionnum);
//		for (LessionTranscript ts:scriptlist)
//		{
//			params.put(ts.getId()+"", ts.getContent());
//		}
		WordUtil.exportWord(sourcePath, scriptlist);		
		insertLessionTransfile(record);
		return 1;
	}
	
	private int insertLessionTransfile(LessionTransfile c) {
		if (c.getId() != null)
		{
			LessionTransfile cc = lessionTransfileMapper.selectByPrimaryKey(c.getId());
			cc.setLessionnum(c.getLessionnum());
			cc.setFilename(c.getFilename());
			cc.setFilesize(c.getFilesize());
			cc.setFilepath(c.getFilepath());
			cc.setModifytime(DateUtils.getDateTime());
			return lessionTransfileMapper.updateByPrimaryKey(cc);
		}
		else
		{
			c.setCreatetime(DateUtils.getDateTime());
			return lessionTransfileMapper.insert(c);
		}
		
	}
}
